$(document).ready(function () {
    editor();
    getData();
});

function editor() {
    edit = new Editor('editor');
    edit.init()
}

function getData() {
    var id = sessionStorage.getItem('mailId');
    console.log(id);
    if (id) {
        /*getAjax('', function (data) {
            viewData(data);
        })*/
    }
}

function viewData(data) {
    $('input[name="recipients"]').val(data.recipients);
    $('input[name="title"]').val(data.title);
    edit.setHtml(data.text);
}

function sendMail() {
    if (validEmail() && validTitle()) {
        var data = {
            recipients: $('input[name="recipients"]').val(),
            title: $('input[name="title"]').val(),
            text: edit.getHtml(),
            attach: ''
        };
        console.log(data);
        /*postAjax('', data, function (data) {
            validData(data, function (result) {
                jumpPage(url)
            })
        })*/
    }
}

function saveDraft() {
    var data = {
        recipients: $('input[name="recipients"]').val(),
        title: $('input[name="title"]').val(),
        text: edit.getHtml(),
        attach: ''
    };
    console.log(data);
    /*postAjax('', data, function (data) {
        validData(data, function (result) {
            jumpPage(url)
        })
    })*/
}

function cancelWrite() {
    clearData();
    backPage();
}

function clearData() {
    $('input').val('');
    edit.clearEditor();
}

function validEmail() {
    var myReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    var recipients = $('input[name="recipients"]').val();
    if (!myReg.test(recipients)) {
        $('input[name="recipients"]').css('border', '1px solid red');
        return false;
    } else {
        $('input[name="recipients"]').css('border', 'none');
        return true;
    }
}

function validTitle() {
    if ($('input[name="title"]').val() == '') {
        $('input[name="title"]').css('border', '1px solid red');
        return false;
    } else {
        $('input[name="title"]').css('border', 'none');
        return true;
    }
}